#ifndef __THREAD_COMM_H__
#define __THREAD_COMM_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

//#include "hiir.h"

/***********************************************************************/

#define RB  "\033[1;31;40m"
#define DE  "\033[0m"

/***********************************************************************/

typedef unsigned char bool;


#define CFG_PATH        "/mnt/mtd/cfg.xml"
#define TMP_CFG_PATH    "/tmp/tmpcfg.xml"
#define UPGRADE_PATH_PR    "/dev/upgrade"
#define UPGRADE_PATH    "/dev/upgrade/upgrade.tar.gz"
#define UPGRADE_FLG_PATH    "/mnt/mtd/upgrade_flg"
#define MAX_RESP_NUM    20
#define GSM_USE_USB     0

#define TRUE  1
#define FALSE 0
#define SMS_CONTENT_MAX_LENGTH 100


#define MSG_REPORT_MAIN(x)      ( (x >> 16) & 0xFFFF )
#define MSG_REPORT_MINOR(x)     ( (x) & 0xFFFF )
#define MSG_REPORT_GEN(x,y)     ( (x << 16) | (y) )

#define DEVICE_ID_MASK 0x00FFFFFF
#define MSG_CONT_LEN    (sizeof(ST_MSG) - sizeof(unsigned int))

typedef enum MSG_T
{
    MSG_T_DUMMY,    /*msg type must be  greater than 0*/
    MSG_T_433M,
    MSG_T_GPIO,
    MSG_T_ALARM,
    MSG_T_SELF_CHECK,
    MSG_T_CAMERA,
    MSG_T_GSM,
    MSG_T_PARSEXML,
    MSG_T_SERIAL,
    MSG_T_UPGRADE,
    MSG_T_MSGMANAGE,
    MSG_T_ADDSENSOR,
    MSG_T_ALL,
    MSG_T_MM_433M,
    MSG_T_MM_GPIO,
    MSG_T_MM_ALARM,
    MSG_T_MM_SELF_CHECK,
    MSG_T_MM_CAMERA,
    MSG_T_MM_GSM,
    MSG_T_MM_PARSEXML,
    MSG_T_MM_SERIAL,
    MSG_T_MM_UPGRADE,
    MSG_T_MM_MSGMANAGE,
    MSG_T_MM_ADDSENSOR
}MSG_T_ENUM;

typedef enum MSG_ST
{
    MSG_ST_NONE = MSG_T_ALL,
/*MSG_T_433M*/

/*MSG_T_MSGMANAGE*/
    MSG_ST_CFG,
/*MSG_T_GPIO*/
    MSG_ST_STATUS_SD,
    MSG_ST_STATUS_ALARM,
    MSG_ST_STATUS_LOCK,
    MSG_ST_STATUS_VIDEO,
    MSG_ST_STATUS_SMS,
    MSG_ST_STATUS_GSM,
    MSG_ST_ALARM_KEY,
/*MSG_T_CAMERA*/
    MSG_ST_SNAPSHOT,
    MSG_ST_RECORD,
/*MSG_T_PARSEXML*/
    MSG_ST_PARSEXML,
/*MSG_T_SERIAL*/
    MSG_ST_STP,
/*MSG_T_GSM*/
    MSG_ST_SMS,
    MSG_ST_MMS,
    MSG_ST_EMAIL,
/*MSG_T_UPGRADE*/
    MSG_ST_UPDATA,
    MSG_ST_ADD_MODE,
    MSG_ST_NORMAL_MODE,
    MSG_ST_SEND_ID,
    MSG_ST_ALL
}MSG_ST_ENUM;


typedef struct st_msg
{
    unsigned int type;
    unsigned int p1;
    void *p2;
}ST_MSG;

typedef struct st_msg_list
{
    struct st_msg msg;
    struct st_msg_list *next;
}ST_MSG_LIST;

typedef struct RF433MSG
{
    unsigned int rfid;
    unsigned int idx;
}RF433MSG_STRU,*pRF433MSG_STRU;

typedef enum uart_comm_type
{
    COMM_GETXML = 0,
    COMM_UPDATEXML,
    COMM_UPGRADE,
    COMM_GETVERSION,
    COMM_ADDSENSOR,
    COMM_ADDCANEL,
    COMM_SENDSENSOR_ID,
    COMM_SETACTION_ID,
    COMM_NUM
} UART_COMM_TYPE;

typedef struct SERIALCMD
{
    UART_COMM_TYPE cmd;
    unsigned int data;
}SERIALCMD_STRU, *pSERIALCMD_STRU;

typedef enum WORK_MODE
{
    EN_ADD_MODE,
    EN_NORMAL_MODE
}WORK_MODE_ENUM;

typedef enum 
{
    CAMERA_NUMBER_NONE = 0,
	CAMERA_NUMBER_1,
	CAMERA_NUMBER_2,
	CAMERA_NUMBER_TOTLE
}EN_CAMERA_NUMBER;

typedef enum
{
	MSG_SECOND_TYPE_GSM_SMS_SELECT_ON,
	MSG_SECOND_TYPE_GSM_MMS_SELECT_ON,
	MSG_SECOND_TYPE_TOTLE
}EN_MSG_SECOND_TYPE;


typedef enum 
{
	CAMERA_NUMBER_1_VIDEO_MODE = 1,		
	CAMERA_NUMBER_1_PHOTO_MODE,
	CAMERA_NUMBER_2_VIDEO_MODE,
	CAMERA_NUMBER_2_PHOTO_MODE,
	CAMERA_SYNC_DATE,
	CAMERA_MODE_TOTLE
}EN_CAMERA_MODE;

typedef enum
{
	EN_ERROR_CODE_SUCCESS,
	EN_ERROR_CODE_FAIL,
	EN_ERROR_CODE_INVALID_PARAM,
	EN_ERROR_CODE_SYSCALL,          /*>system call error*/
	EN_ERROR_CODE_GSM_INIT_FAILED,
	EN_ERROR_CODE_MSG_TYPE_FAIL,
	EN_ERROR_CODE_MSG_DATA_FAIL,
	EN_ERROR_CODE_XML_PARSE,
	EN_ERROR_CODE_TOTLE
}EN_ERROR_CODE;

#define MAX_DEFENCE_NUM     20
#define MAX_ACTION_NUM      20
#define MAX_CS_NUM          10
#define MAX_REMOTE_NUM      5

typedef enum SECURITYTYPE
{
    SECURITY_INDOOR,
    SECURITY_OUTDOOR,
    SECURITY_24H,
    SECURITY_NUM
}SECURITYTYPE_ENUM;

typedef struct DAILY
{
    unsigned int balance;
    bool defence_status;
    unsigned char signal_strength;
    unsigned char battery_info;
} DAILY;

typedef struct ALARM_SETTING
{
    char low_signal_enable;
    unsigned char low_signal_threshold;
    unsigned short defence_delay;
    unsigned short undefence_delay;
    char is_use_alarm;
} ALARM_SETTING;

typedef struct EMAILSETTING
{
    char smtpurl[128];
    unsigned short smtpport;
    char user[64];
    char passwd[64];
}EMAILSETTING_STRU;

typedef struct EMAILCONT
{
    char to[64];
    char cc[MAX_CS_NUM][64];
    char title[128];
    char cont[1023];
    char attapath[128];
}EMAILCONT_STRU;

typedef struct DEFENCE_AREA_T
{
    unsigned int device_id;
    unsigned char index;
    unsigned char del;
    unsigned char name[64];
    unsigned char enable:1;
    unsigned char type:2;        /*0:indoor   1:outdoor  2:24h see: SECURITYTYPE_ENUM*/
    unsigned char camera:2;      /*0:unused 1: first 2:second*/
    unsigned char snapshot:1;    /*0:disable 1:enable*/
    unsigned char record:1;      /*0:disable 1:enable*/
    unsigned char sms:1;
    unsigned char mms:1;
    unsigned char email:1;
    unsigned char alarm:1;
    unsigned char discrible[128];
} DEFENCE_AREA_T;

typedef struct SMS_SETTING
{
    char time_sync_num[18];
    char smsc[18];
} SMS_SETTING;

typedef struct SYS_MMS_SETTING
{
    char mmsurl[50];
    char gateway[15];
    char apn[64];
    unsigned short port;
} SYS_MMS_SETTING;

typedef struct GSM_SETTING
{
    SMS_SETTING sms;
    SYS_MMS_SETTING mms;
} GSM_SETTING;


typedef struct SNAPSHOT_SETTING
{
    char cap_num;
} SNAPSHOT_SETTING;

typedef struct RECORD_SETTING
{
    unsigned int duration;
} RECORD_SETTING;

typedef struct CAMERA_SETTING
{
    SNAPSHOT_SETTING snapshot;
    RECORD_SETTING  record;
} CAMERA_SETTING;

typedef struct ACTION_DEVICE
{
    unsigned char index;
    unsigned char del;
    unsigned int id;
    unsigned char name[32];
    unsigned char description[128];
    unsigned char enable;
}ACTION_DEVICE_STRU;

typedef struct BASE_SETTING
{
    DAILY daily;
    ALARM_SETTING alarm_setting;
    EMAILSETTING_STRU email_setting;
} BASE_SETTING;


typedef struct RESPONSE_SETTING
{
    GSM_SETTING gsm;
    CAMERA_SETTING camera;
} RESPONSE_SETTING;


typedef struct CS_SETTING
{
    unsigned char index;
    unsigned char del;
    unsigned char name[64];
    unsigned char phone[16];
    unsigned char email[64];
    unsigned char sms_en;
    unsigned char mms_en;
    unsigned char email_en;
} CS_SETTING;

typedef struct REMOTE
{
  unsigned char index;
  unsigned char del;
  unsigned int defence;
  unsigned int undefence;
  unsigned int emergency;
  unsigned int athome;
}REMOTE_STRU;

typedef struct SYSTEM_CFG
{
    BASE_SETTING base_setting;
    REMOTE_STRU remote[MAX_REMOTE_NUM];
    DEFENCE_AREA_T defence_area[MAX_DEFENCE_NUM];
    ACTION_DEVICE_STRU actiondevice[MAX_ACTION_NUM];
    RESPONSE_SETTING response_setting;
    CS_SETTING cs_setting[MAX_CS_NUM];
} SYSTEM_CFG;

typedef enum DEFENCEMODE
{
    EN_DEFENCE_ON,
    EN_DEFENCE_OFF,
    EN_DEFENCE_ATHOME,
    EN_DEFENCE_NUM
}DEFENCEMODE_ENUM;

extern SYSTEM_CFG syscfg;
extern char get_current_device_index();
DEFENCEMODE_ENUM get_current_security_mode( void );

extern bool get_video_status(void);

//extern bool get_status_pMsgListArray(void);

extern bool get_status_thread_camera(void);
extern bool get_status_thread_gsm(void);
extern bool get_status_thread_alarm(void);


	
#endif

